/*
 * ========================================================
 * Copyright(c) 2014 杭州偶尔科技-版权所有
 * ========================================================
 * 本软件由杭州偶尔科技所有, 未经书面许可, 任何单位和个人不得以
 * 任何形式复制代码的部分或全部, 并以任何形式传播。
 * 公司网址
 * 
 * 			http://www.kkkd.com/
 * 
 * ========================================================
 */
package com.simon.mengkou.ui.adapter;

import android.content.Context;
import android.os.Handler;
import android.support.v4.content.ContextCompat;
import android.text.Html;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.common.soloader.SoLoaderShim;
import com.facebook.drawee.view.SimpleDraweeView;
import com.ouertech.android.agm.lib.base.utils.UtilLog;
import com.simon.mengkou.R;
import com.simon.mengkou.data.bean.base.GroupBuy;
import com.simon.mengkou.data.bean.base.Order;
import com.simon.mengkou.data.bean.base.Product;
import com.simon.mengkou.data.enums.EGroupBuyStatus;
import com.simon.mengkou.data.enums.EOrderStatus;
import com.simon.mengkou.system.global.OuerApplication;
import com.simon.mengkou.system.global.OuerDispatcher;
import com.simon.mengkou.ui.dialog.GroupBuyCancelDialog;
import com.simon.mengkou.ui.fragment.GroupBuyFragment;
import com.simon.mengkou.ui.view.MyListView;
import com.simon.mengkou.utils.UtilOuer;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * @author : Zhenshui.Xia
 * @since : 2016/3/27.
 * desc    :
 */
public class GroupBuyAdapter extends AbsAdapter<GroupBuy> {
    private GroupBuyFragment mFragment;
    private int mRedColor;
    private int mGreenColor;
    private int mGrayColor;

    public GroupBuyAdapter(Context context, GroupBuyFragment fragment) {
        super(context);
        mFragment = fragment;
        mRedColor = ContextCompat.getColor(context, R.color.res_color_primary);
        mGreenColor = ContextCompat.getColor(context, R.color.res_color_accent);
        mGrayColor = ContextCompat.getColor(context, R.color.res_color_black_secondary_text);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup arg2) {
        final Holder holder;
        if (null == convertView) {
            convertView = mInflater.inflate(R.layout.layout_group_buy_item, null);
            holder = new Holder();
            holder.mHandler = new Handler();
            convertView.setTag(holder);
            ButterKnife.bind(holder, convertView);
        } else {
            holder = (Holder) convertView.getTag();
        }

        final GroupBuy data = getItem(position);
        //打开详情
        holder.mLlRoot.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                OuerDispatcher.presentOrderDetailActivity(mContext, data.getOrderId(), data.getId());
            }
        });

        //倒计时
        if(EGroupBuyStatus.PENDING == data.getStatus()) {
            holder.mTvStatus.setVisibility(View.VISIBLE);
            Runnable runnable = (Runnable) holder.mTvStatus.getTag();
            if(runnable != null) {
                holder.mHandler.removeCallbacks(runnable);
            }

            runnable = new Runnable() {
                @Override
                public void run() {
                    long left = data.getDeadline() - System.currentTimeMillis();
                    if(left <= 0) {
                        left = 0;
                    }

                    holder.mTvStatus.setText(mContext.getString(R.string.group_group_buy_time,
                            UtilOuer.formatCounterTime(left)));
                    if(left>=1000) {
                        holder.mHandler.postDelayed(this, 1000);
                    } else {
                        holder.mHandler.removeCallbacks(this);
                        OuerDispatcher.sendGroupFinishBroadcast(mContext, data.getId());
                    }
                }
            };
            holder.mTvStatus.setTag(runnable);
            holder.mHandler.postDelayed(runnable, 0);

        } else {
            holder.mTvStatus.setVisibility(View.GONE);
            Runnable runnable = (Runnable) holder.mTvStatus.getTag();
            if(runnable != null) {
                holder.mHandler.removeCallbacks(runnable);
            }
        }


        final Product product = data.getGoods();
        //图片
        OuerApplication.mImageLoader.loadImage(holder.mSdvImage, product.getImage().getUrl());
        holder.mSdvImage.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                OuerDispatcher.presentProductDetailActivity(mContext, product.getId());
            }
        });
        //标题
        holder.mTvTitle.setText(product.getTitle());
        //价格
        holder.mTvPrice.setText(mContext.getString(R.string.common_price,
                UtilOuer.formatPrice(product.getPrice())));

        //实付
        holder.mTvAmount.setText(Html.fromHtml(mContext.getString(R.string.my_order_amount,
                UtilOuer.formatPrice(data.getAmount()))));

        //取消拼团
        int status = data.getStatus();
        if(EGroupBuyStatus.PENDING == status) {
            //取消拼团
            holder.mBtnCancel.setVisibility(View.VISIBLE);
            holder.mBtnCancel.setText(R.string.group_group_buy_cancel);
            holder.mBtnCancel.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    new GroupBuyCancelDialog(mFragment, data).show();
                }
            });

            holder.mTvNum.setText(R.string.group_group_buy_pending);
            holder.mTvNum.setTextColor(mRedColor);
        } else if(EGroupBuyStatus.FINISH == status) {
            holder.mBtnCancel.setVisibility(View.GONE);
            holder.mTvNum.setText(R.string.group_group_buy_success);
            holder.mTvNum.setTextColor(mGreenColor);
        } else if(EGroupBuyStatus.CANCEL == status) {
            holder.mBtnCancel.setVisibility(View.GONE);
            holder.mTvNum.setText(R.string.group_group_buy_canceled);
            holder.mTvNum.setTextColor(mGrayColor);
        } else {
            holder.mBtnCancel.setVisibility(View.GONE);
            holder.mTvNum.setText(R.string.group_group_buy_failure);
            holder.mTvNum.setTextColor(mGrayColor);
        }

        return convertView;
    }


    /**
     * @author zhenshui.xia
     */
    class Holder {
        //
        @Bind(R.id.order_id_root)
        LinearLayout mLlRoot;
        //图片
        @Bind(R.id.order_id_image)
        SimpleDraweeView mSdvImage;
        //标题
        @Bind(R.id.order_id_title)
        TextView mTvTitle;
        //价格
        @Bind(R.id.order_id_price)
        TextView mTvPrice;
        //时间
        @Bind(R.id.order_id_status)
        TextView mTvStatus;
        //实付
        @Bind(R.id.order_id_amount)
        TextView mTvAmount;
        //取消
        @Bind(R.id.order_id_cancel)
        Button mBtnCancel;
        //状态
        @Bind(R.id.order_id_num)
        TextView mTvNum;

        Handler mHandler;
    }
}
